package com.example.myweather.Activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myweather.Adapter.CityAdapter
import com.example.myweather.R
import com.example.myweather.ViewModel.CityViewModel
import com.example.myweather.databinding.ActivityCityListBinding
import com.example.myweather.model.CityResponseApi
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class CityListActivity : AppCompatActivity() {
    // 使用 View Binding 来替代 findViewById，简化视图绑定操作
    lateinit var binding:ActivityCityListBinding
    // 初始化适配器用于 RecyclerView
    private val cityAdapter by lazy { CityAdapter() }
    // 使用 AndroidX 的 viewModels() 方法来获取 ViewModel 实例
    private val cityViewHolder:CityViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 通过 View Binding 获取布局实例
        binding=ActivityCityListBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // 设置窗口属性，这里设置了系统栏的背景绘制标志
        window.apply {
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

        }
        // 为 EditText 添加文本变化监听器
        binding.apply {
            cityEdit.addTextChangedListener(object :TextWatcher{
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    before: Int,
                    count: Int) {
                    // 在文本变化之前调用，此处不需要处理
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    // 在文本变化时调用，此处不需要处理
                }

                override fun afterTextChanged(s: Editable?) {
                    // 当文本变化之后调用

                    progressBar2.visibility=View.VISIBLE// 显示进度条
                    // 调用 ViewModel 方法加载城市数据
                    cityViewHolder.loadCity(s.toString(),10)
                        .enqueue(object :Callback<CityResponseApi>{
                        override fun onResponse(
                            call: Call<CityResponseApi>,
                            response: Response<CityResponseApi>
                        ) {
                            if(response.isSuccessful){
                                // 获取响应数据
                                val data=response.body()
                                data?.let {
                                    progressBar2.visibility=View.GONE// 隐藏进度条
                                    // 提交列表数据给适配器，并设置 RecyclerView
                                    cityAdapter.differ.submitList(it)
                                    cityView.apply {
                                        layoutManager=LinearLayoutManager(
                                            this@CityListActivity,
                                            LinearLayoutManager.HORIZONTAL,
                                            false)
                                        adapter=cityAdapter
                                    }
                                }
                            }
                        }

                        override fun onFailure(call: Call<CityResponseApi>, t: Throwable) {

                        }

                    })
                }

            }) }
    }
}